/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MantEnunciado.java
 *
 * Created on 12/09/2011, 03:52:06 PM
 */

package GUI.AdministracionPlantillas;

import BE.Enunciado;
import BE.Seccion;
import BL.EnunciadoBl;
import GUI.AdministracionUsuarios.UtilUsuarios;
import GUI.Principal;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author CarLita
 */
public class MantEnunciado extends javax.swing.JFrame {
    Seccion seccion;
    EnunciadoBl enunciadoBl;
    int modoVentana;
    Principal ventanaPrincipal;
    
    /** Creates new form MantEnunciado */
    public MantEnunciado(Seccion seccion, Principal ventanaPrincipal) {
        this.seccion = seccion;
        this.ventanaPrincipal = ventanaPrincipal;
        System.out.println("Id seccion: "+ seccion.getIdSeccion()+" - " +seccion.getTitulo());
        enunciadoBl = new EnunciadoBl();        
        initComponents();
        modoVentana = Principal.MODO_CREACION;
        setearModo(Principal.MODO_CREACION);
         bloquearPorPermisos();
    }

    MantEnunciado(Enunciado e, int modo) {
        initComponents();
        txtEnunciado.setText(e.getEnunciado());
        enunciadoBl = new EnunciadoBl();
        setearModo(modo);
    }

    private void setearModo(int modo){
        modoVentana = modo;
        switch(modo){
            case Principal.MODO_CREACION:
                btnEditar.setVisible(false);
                btnLimpiar.setVisible(true);
                btnGuardar.setVisible(true);
                break;
            case Principal.MODO_EDICION:
                btnEditar.setVisible(false);
                btnLimpiar.setVisible(false);
                btnGuardar.setVisible(true);
                break;
            case Principal.MODO_LECTURA:
                btnEditar.setVisible(true);
                btnLimpiar.setVisible(false);
                btnGuardar.setVisible(false);
                break;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtEnunciado = new javax.swing.JTextArea();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel4.setFont(new java.awt.Font("Verdana", 0, 24));
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/imagenes/lista-de-regalos.png"))); // NOI18N
        jLabel4.setText("Enunciado");

        jLabel1.setText("Enunciado:");

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/guardar.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/limpiar.png"))); // NOI18N
        btnLimpiar.setText("Limpiar");

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/cancelar.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/edit.gif"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        txtEnunciado.setColumns(20);
        txtEnunciado.setFont(new java.awt.Font("Tahoma", 0, 11));
        txtEnunciado.setRows(5);
        jScrollPane2.setViewportView(txtEnunciado);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLimpiar)
                    .addComponent(btnCancelar)
                    .addComponent(btnGuardar)
                    .addComponent(btnEditar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        setearModo(Principal.MODO_EDICION);
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        guardarEnunciado();
    }//GEN-LAST:event_btnGuardarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new MantEnunciado(seccion).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea txtEnunciado;
    // End of variables declaration//GEN-END:variables

    private void guardarEnunciado() {
        Enunciado e = new Enunciado();
        e.setEnunciado(txtEnunciado.getText().trim());        
        if (modoVentana == Principal.MODO_CREACION){
            e.setSeccionByIdSeccion1(seccion);
            e.setCorrelativo(calcularCorrelativo());                                  
        }
        System.out.println("Seccion: "+e.getSeccionByIdSeccion1().getIdSeccion());
        boolean guardado = enunciadoBl.guardar(e);

        if (guardado){
            JOptionPane.showMessageDialog(this, "El enunciado ha sido creado.", "Confirmación", JOptionPane.INFORMATION_MESSAGE);
            if (modoVentana==Principal.MODO_CREACION ){
                setearModo(Principal.MODO_EDICION);
            }            
        }
         else {
            JOptionPane.showMessageDialog(this, "Ha ocurrido un error en la grabacion.\n Por favor, intente nuevamente.", "Error", JOptionPane.ERROR_MESSAGE);
         }
    }

    private int calcularCorrelativo() {
        List l = enunciadoBl.buscarPorSeccion(seccion.getIdSeccion());        
        return l.size()+1;
    }

    private void bloquearPorPermisos() {
         if (ventanaPrincipal.getPermisos().getPermisoPapel().indexOf(UtilUsuarios.VER)>-1){
            btnEditar.setVisible(false);
            btnEditar.setEnabled(false);
            btnGuardar.setVisible(false);
            btnGuardar.setEnabled(false);
        }        
        if (ventanaPrincipal.getPermisos().getPermisoPapel().indexOf(UtilUsuarios.CREAR)>-1){
            btnEditar.setVisible(false);
            btnEditar.setEnabled(false);
            btnGuardar.setVisible(true);
            btnGuardar.setEnabled(true);
        }                
        if (ventanaPrincipal.getPermisos().getPermisoPapel().indexOf(UtilUsuarios.EDITAR)>-1){
            btnEditar.setVisible(true);
            btnEditar.setEnabled(true);
            btnGuardar.setVisible(true);
            btnGuardar.setEnabled(true);
        }
    }

}
